import olproj from 'ol/proj'
// import * as opl from 'ol/proj'
import TileLayer from 'ol/layer/tile'
import TileGrid from 'ol/tilegrid/tilegrid'
import TileImage from 'ol/source/tileimage'
import projzh from 'projzh'
import olextent from 'ol/extent'
import Projection from 'ol/proj/projection'

/**
 * 百度地图坐标转换扩展
 */

const extent = [72.004, 0.8293, 137.8347, 55.8271]
// 定义百度坐标
// 地址：https://github.com/openlayers/openlayers/issues/3522
const baiduMercator = new Projection({
  code: 'baidu',
  extent: olextent.applyTransform(extent, projzh.ll2bmerc),
  units: 'm'
})
olproj.addProjection(baiduMercator)
olproj.addCoordinateTransforms('EPSG:4326', baiduMercator, projzh.ll2bmerc, projzh.bmerc2ll)
olproj.addCoordinateTransforms('EPSG:3857', baiduMercator, projzh.smerc2bmerc, projzh.bmerc2smerc)

/**
 * 百度地图来源
 * */
function baiduSource () {
  var resolutions = []
  for (var i = 0; i < 20; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }
  var tilegrid = new TileGrid({
    origin: [0, 0],
    resolutions: resolutions,
    extent: olextent.applyTransform(extent, projzh.ll2bmerc)
  })
  var baiduSource = new TileImage({
    // projection: olproj.get('EPSG:3857'),
    projection: 'baidu',
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = 'M' + (-x)
      }
      if (y < 0) {
        y = 'M' + (-y)
      }
      return 'http://api0.map.bdimg.com/customimage/tile?&scaler=1qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=t%3Awater%7Ce%3Aall%7Cc%3A%233287cfff%2Ct%3Ahighway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Ahighway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%23147a92%2Ct%3Aarterial%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Aarterial%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%230b3d51%2Ct%3Alocal%7Ce%3Ag%7Cc%3A%23000000%2Ct%3Aland%7Ce%3Aall%7Cc%3A%2307114aff%2Ct%3Arailway%7Ce%3Ag.f%7Cv%3Aoff%7Cc%3A%23000000%2Ct%3Arailway%7Ce%3Ag.s%7Cv%3Aoff%7Cc%3A%2308304b%2Ct%3Asubway%7Ce%3Ag%7Cv%3Aoff%7Cl%3A-70%2Ct%3Abuilding%7Ce%3Ag.f%7Cc%3A%23000000%2Ct%3Aall%7Ce%3Al.t.f%7Cc%3A%23857f7f%2Ct%3Aall%7Ce%3Al.t.s%7Cc%3A%23000000%2Ct%3Abuilding%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Agreen%7Ce%3Ag%7Cc%3A%23062032%2Ct%3Aboundary%7Ce%3Aall%7Cc%3A%2300ffffff%2Ct%3Amanmade%7Ce%3Ag%7Cc%3A%23022338%2Ct%3Apoi%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.i%7Cv%3Aoff%2Ct%3Aall%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%232da0c6'
    }
  })
  return baiduSource
}

export const baiduLayer = new TileLayer({
  source: baiduSource()
})
baiduLayer.imgsrc = '/static/img/blue-map.png'
baiduLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  baiduLayer.setVisible(true)
}
// 白色
/* ===========百度地图来源结束============= */
/**
 * 百度地图来源
 * */
function baiduWSource () {
  var resolutions = []
  for (var i = 0; i < 20; i++) {
    resolutions[i] = Math.pow(2, 18 - i)
  }
  var tilegrid = new TileGrid({
    origin: [0, 0],
    resolutions
  })
  var baiduSource = new TileImage({
    // projection: olproj.get('EPSG:3857'),
    projection: 'baidu',
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = tileCoord[1]
      var y = tileCoord[2]
      if (x < 0) {
        x = 'M' + (-x)
      }
      if (y < 0) {
        y = 'M' + (-y)
      }
      return 'http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=' + x + '&y=' + y + '&z=' + z + '&styles=pl&scaler=1&p=1'
    }
  })
  return baiduSource
}
// 百度地图白色
export const baiduWLayer = new TileLayer({
  source: baiduWSource()
})
// baiduWLayer.imgsrc = '/static/img/baidu-white-map.png'
baiduWLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  baiduWLayer.setVisible(true)
}
/* ===========百度地图来源结束============= */

/**
 * 规划局服务
 */
function planSource () {
  // 获取坐标系参数
  var projection = olproj.get('EPSG:4326')
  // 分辨率
  var resolutions = [
    0.0027363801567048224,
    0.0017845957543727103,
    0.00118973050291514,
    7.138383017490841e-4,
    3.5691915087454206e-4,
    1.6656227040811962e-4,
    8.328113520405981e-5,
    5.9486525145757005e-5,
    4.1640567602029906e-5,
    2.3794610058302804e-5,
    1.7845957543727102e-5,
    1.1897305029151402e-5,
    8.32811352040598e-6,
    4.75892201166056e-6,
    2.37946100583028e-6
  ]
  // 贴图组
  var tilegrid = new TileGrid({
    origin: [-400, 400],
    resolutions: resolutions,
    tileSize: 256
  })
  // 地图服务来源
  var planSource = new TileImage({
    projection: projection,
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = Math.abs(tileCoord[1])
      var y = Math.abs(tileCoord[2]) - 1

      return (
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' +
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/'+
        'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/' + z + '/' + y + '/' + x)
    }
  })
  return planSource
}
// 构建地图图层
export const planLayer = new TileLayer({
  source: planSource()
})
planLayer.imgsrc = '/static/img/white-map.png'
planLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  planLayer.setVisible(true)
}
/* ===============规划局底图================= */

/**
 * 规划局服务
 */
function rsSource () {
  // 获取坐标系参数
  var projection = olproj.get('EPSG:4326')
  // 分辨率
  var resolutions = [
    0.0027363801567048224,
    0.0017845957543727103,
    0.00118973050291514,
    7.138383017490841e-4,
    3.5691915087454206e-4,
    1.6656227040811962e-4,
    8.328113520405981e-5,
    5.9486525145757005e-5,
    4.1640567602029906e-5,
    2.3794610058302804e-5,
    1.7845957543727102e-5,
    1.1897305029151402e-5,
    8.32811352040598e-6,
    4.75892201166056e-6,
    2.37946100583028e-6
  ]
  // 贴图组
  var tilegrid = new TileGrid({
    origin: [-400, 400],
    resolutions: resolutions,
    tileSize: 256
  })
  // 地图服务来源
  var planSource = new TileImage({
    projection: projection,
    tileGrid: tilegrid,
    tileUrlFunction: function (tileCoord, pixelRatio, proj) {
      if (!tileCoord) {
        return ''
      }
      var z = tileCoord[0]
      var x = Math.abs(tileCoord[1])
      var y = Math.abs(tileCoord[2]) - 1

      return (
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' +
      // 'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/政务电子地图/MapServer/tile/'+
        'http://10.32.208.34:6080/whgis/rest/services/BaseGeography/2015年卫星影像/MapServer/tile/' + z + '/' + y + '/' + x)
    }
  })
  return planSource
}
// 构建地图图层
export const rsLayer = new TileLayer({
  source: rsSource()
})
rsLayer.imgsrc = '/static/img/RS-map.png'
rsLayer.clickFun = function (layers) {
  for (let layer of layers) {
    layer.setVisible(false)
  }
  rsLayer.setVisible(true)
}

/* ===============规划局底图================= */

// 百度地图（白色）初始状态隐藏
baiduWLayer.setVisible(false)
// 百度地图（黑色）初始状态隐藏
baiduLayer.setVisible(false)
// 规划局初始状态隐藏
planLayer.setVisible(false)
// 卫星遥感初始状态隐藏
rsLayer.setVisible(false)

export default [baiduLayer, baiduWLayer, planLayer, rsLayer]
